package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 目标：
 *
 * @author Ashely
 * @date 2024/6/17 16:29
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Override
    public void add(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.add(addressBook);

    }

    @Override
    public List<AddressBook> list(AddressBook addressBook) {
        List<AddressBook> list = addressBookMapper.list(addressBook);
        return list;
    }

    @Override
    public AddressBook getById(Long id) {
    AddressBook addressBook =  addressBookMapper.getById(id);
    return addressBook;
    }

    @Override
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    @Override
    public void delete(Long id) {
        addressBookMapper.delete(id);
    }

    @Override
    public void setDefault(AddressBook addressBook) {
        AddressBook build = AddressBook.builder().userId(BaseContext.getCurrentId()).build();
        List<AddressBook> list = addressBookMapper.list(build);
        if (list != null && list.size() > 0) {
            for (AddressBook adBook : list) {
                adBook.setIsDefault(0);
                addressBookMapper.update(adBook);
            }
            addressBook.setIsDefault(1);
            addressBookMapper.update(addressBook);
        }

        

    }

}
